﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Helper.DateTime
{
    public class TimeTransmit
    {
        static TimeTransmit _timeTransimt = null;

        public static TimeTransmit Instance
        {
            get
            {
                if (_timeTransimt == null)
                {
                    _timeTransimt = new TimeTransmit();
                }
                return _timeTransimt;
            }
        }

        /// <summary>
        /// fnTimeConverter
        /// </summary>
        /// <param name="strTime"></param>
        /// <param name="sb"></param>
        /// <returns></returns>
        public int fnTimeConverterEx(string strTime, StringBuilder sb)
        {
            System.DateTime dt = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
            dt = dt.AddSeconds(double.Parse(strTime));
            dt = dt.AddHours(8);
            string str = dt.ToString("yyyy-MM-dd HH:mm:ss,fff");
            sb.Append(str);
            return 1;
            //return fnTimeConverter(strTime, sb);
        }

        [DllImport("dx.dll", EntryPoint = "fndx", SetLastError = true, CharSet = CharSet.Ansi)]
        static extern int fnTimeConverter(string strTime, StringBuilder sb);

        public string StringToJsTime(string totime)
        {
            System.DateTime dt = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);
            dt =dt.AddMilliseconds(double.Parse(totime));
            dt = dt.AddHours(8);

            string strTime = dt.ToString("yyyy-MM-dd HH:mm:ss,fff");

            return strTime;
            //string strScript = @"function getLocalTime() {return new Date(parseInt(" + totime + ")).toLocaleString(); }";
            //object oo = Script.Script.Instance.Execute(strScript, "getLocalTime()");
            //if (oo == null)
            //{
            //    return "";
            //}
            //else
            //{
            //    return oo.ToString();
            //}
        }

        public string DateTimeToString()
        {
            return getDate();
            //string strScript = @"function getLocalTime() {return  + new Date(); }";
            //object oo = Script.Script.Instance.Execute(strScript, "getLocalTime()");
            //if (oo == null)
            //{
            //    return "";
            //}
            //else
            //{
            //    return oo.ToString();
            //}
        }

        public string getDate()
        {
            System.DateTime Time1 = System.DateTime.UtcNow;
            System.DateTime Time2 = Convert.ToDateTime("1970-01-01");
            TimeSpan span = Time1 - Time2;
            string t = span.TotalMilliseconds.ToString("0");    
            return t;
            //string code = "function getDate(){return new Date().getTime();}";
            //object o =  Script.Script.Instance.Execute(code, "getDate()");
            //if (o == null)
            //{
            //    return "";
            //}
            //else
            //{
            //    return o.ToString();
            //}
            
        }

       

    }
}
